﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Portal.Web.Controllers
{
    public class ProductController : BaseFrontController
    {

        public ProductController(IProductRepository productRepository)
        {
            _productRepository = productRepository;
        }
        private readonly IProductRepository _productRepository;
        public ActionResult Index(string categoryId, string name = "", string sort = "CreateTime", string order = "DESC", int page = 1, int rows = 8)
        {
            var model = CreateFrontViewData<FronProductViewData>();
            model.CurrentCategory = Category.FirstOrDefault(o => o.Id == categoryId);
            model.List =
                _productRepository.Get(o => (string.IsNullOrEmpty(categoryId) || o.CategoryId == categoryId) && (string.IsNullOrEmpty(name) || o.Name.Contains(name.Trim()))).Sorting(sort, order).ToPagedList(page, rows);
            if (!model.List.Any())
                return NotFount();
            return View(model);
        }
        /// <summary>
        /// 产品中心
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public ActionResult Details(string productId)
        {
            var model = CreateFrontViewData<FronProductViewData>();
            model.Model = _productRepository.Get().FirstOrDefault(o => o.Id == productId);
            if (model.Model == null)
                return NotFount();
            return View(model);
        }

        /// <summary>
        /// 产品滚动列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ProductListScrollControl(int count = 1)
        {
            var model = CreateFrontViewData<FronProductViewData>();
            model.List = _productRepository.Get(o => o.IsRecommend).OrderByDescending(o => o.CreateTime).Take(count).ToList();
            return View(model);
        }

        /// <summary>
        /// 推荐产品不滚动
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public ActionResult RecommendedProductsControl(int count = 1)
        {
            var model = CreateFrontViewData<FronProductViewData>();
            model.List = _productRepository.Get(o => o.IsRecommend).OrderByDescending(o => o.CreateTime).Take(count).ToList();
            return View(model);
        }


        /// <summary>
        /// 没有找到产品
        /// </summary>
        /// <returns></returns>
        public ActionResult NotFount()
        {
            var model = CreateFrontViewData<FronProductViewData>();
            return View(model);
        }

    }
}
